package ch14;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class ToHTML {
    public static void main(String[] args) throws Exception {
        tohtml(args[0], args[1]);
        System.out.println("stop");
    }

    static void tohtml(String infile, String outfile) throws Exception {
        FileReader fr_1 = new FileReader(infile);
        BufferedReader in_1 = new BufferedReader(fr_1);
        FileWriter fw_1 = new FileWriter(outfile);
        BufferedWriter bw_1 = new BufferedWriter(fw_1);

        String line = in_1.readLine();
        String src = "";
        while (line != null) {
            src += line + "\n";
            line = in_1.readLine();
        }
        in_1.close();

        String[] keywords = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class",
                "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for",
                "if", "implements", "import", "int", "interface", "instanceof", "long", "native", "new", "package",
                "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch",
                "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "goto",
                "const", "true", "false", "null" };

        src = src.replaceAll("<", "&lt;");
        src = src.replaceAll(">", "&gt;");

        for (String keyword : keywords) {
            src = src.replaceAll("\\b" + keyword + "\\b", "<span class=keyword>" + keyword + "</span>");
        }

        src = src.replaceAll("\\b((\\-|\\+)?\\d+(\\.\\d+)?)\\b", "<span class=var>" + "$0" + "</span>");
        src = src.replaceAll("(\"[^\"]*\")", "<span class=var>" + "$0" + "</span>");

        src = src.replaceAll("(//.*\\n)", "<span class=comment>" + "$0" + "</span>");
        src = src.replaceAll("(/\\*(.|\\n)*\\*/)", "<span class=comment>" + "$0" + "</span>");

        bw_1.write("<!DOCTYPE html>\n");
        bw_1.write("<html lang=\"en\">\n");
        bw_1.write("<head>\n");
        bw_1.write("    <meta charset=\"UTF-8\">\n");
        bw_1.write("    <title>Java源码</title>\n");
        bw_1.write("    <style type=\"text/css\">\n");
        bw_1.write("        .keyword{font-weight: bold; color: blue;}\n");
        bw_1.write("        .comment{font-style: italic; color: silver;}\n");
        bw_1.write("        .var{color: red;}\n");
        bw_1.write("    </style>\n");
        bw_1.write("</head>\n");
        bw_1.write("<body>\n");
        bw_1.write("    <pre>\n");

        bw_1.write(src);

        bw_1.write("    </pre>\n");
        bw_1.write("</body>\n");
        bw_1.write("</html>\n");
        bw_1.flush();
        bw_1.close();
    }
}